package cn.sunyblog.tool.threadpool;

import cn.sunyblog.tool.threadpool.config.DynamicThreadPoolConfig;
import cn.sunyblog.tool.threadpool.strategy.DefaultAdjustmentStrategy;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 简单测试类，验证动态线程池的扩容逻辑
 */
public class SimpleTest {
    
    public static void main(String[] args) throws InterruptedException {
        System.out.println("=== 动态线程池扩容逻辑测试 ===");
        
        // 创建配置
        DynamicThreadPoolConfig config = new DynamicThreadPoolConfig();
        config.setThreadPoolName("test-pool");
        config.setCorePoolSize(2);
        config.setMaximumPoolSize(10);
        config.setQueueCapacity(5);
        config.setMonitorInterval(5); // 5秒监控间隔
        config.setEnableAutoAdjustment(true);
        
        // 创建动态线程池
        DynamicThreadPoolExecutor executor = new DynamicThreadPoolExecutor(config);
        
        // 创建任务计数器
        AtomicInteger taskCounter = new AtomicInteger(0);
        AtomicInteger completedTasks = new AtomicInteger(0);
        
        System.out.println("初始配置: 核心线程=" + config.getCorePoolSize() + 
                          ", 最大线程=" + config.getMaximumPoolSize() + 
                          ", 队列容量=" + config.getQueueCapacity());
        
        // 监控会自动启动
        
        // 模拟持续高压场景
        System.out.println("\n开始持续高压测试...");
        
        // 第一波：快速提交大量任务
        for (int i = 0; i < 50; i++) {
            final int taskId = taskCounter.incrementAndGet();
            try {
                executor.submit(() -> {
                    try {
                        System.out.println("任务 " + taskId + " 开始执行");
                        Thread.sleep(2000); // 模拟耗时任务
                        completedTasks.incrementAndGet();
                        System.out.println("任务 " + taskId + " 完成");
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
            } catch (Exception e) {
                System.out.println("任务 " + taskId + " 被拒绝: " + e.getMessage());
            }
            
            // 每提交10个任务后稍微暂停
            if (i % 10 == 9) {
                Thread.sleep(100);
            }
        }
        
        System.out.println("第一波任务提交完成，等待15秒观察扩容...");
        Thread.sleep(15000);
        
        // 第二波：继续提交任务
        System.out.println("\n开始第二波任务提交...");
        for (int i = 0; i < 30; i++) {
            final int taskId = taskCounter.incrementAndGet();
            try {
                executor.submit(() -> {
                    try {
                        System.out.println("任务 " + taskId + " 开始执行");
                        Thread.sleep(1500);
                        completedTasks.incrementAndGet();
                        System.out.println("任务 " + taskId + " 完成");
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                });
            } catch (Exception e) {
                System.out.println("任务 " + taskId + " 被拒绝: " + e.getMessage());
            }
            
            if (i % 5 == 4) {
                Thread.sleep(50);
            }
        }
        
        System.out.println("第二波任务提交完成，等待20秒观察扩容...");
        Thread.sleep(20000);
        
        // 打印最终统计
        System.out.println("\n=== 测试结果 ===");
        System.out.println("总提交任务: " + taskCounter.get());
        System.out.println("完成任务: " + completedTasks.get());
        System.out.println("当前核心线程数: " + executor.getCorePoolSize());
        System.out.println("当前最大线程数: " + executor.getMaximumPoolSize());
        System.out.println("当前活跃线程数: " + executor.getActiveCount());
        System.out.println("当前队列大小: " + executor.getQueue().size());
        
        // 关闭线程池
        executor.shutdown();
        try {
            if (!executor.awaitTermination(10, TimeUnit.SECONDS)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            executor.shutdownNow();
        }
        
        System.out.println("测试完成");
    }
}